namespace AsyncFactory
{
    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;

    public class AsyncProcess
    {
        public static void Process(AsyncProcessHandle pHandle, AsyncProcessCompletedHandle cHandle = null, bool isModel = false, LoadingImageType loadingImageType = 0)
        {
            FrmLoading frmLoading = null;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (obj, ergs) => pHandle();
            worker.RunWorkerCompleted += delegate (object obj, RunWorkerCompletedEventArgs ergs) {
                if (frmLoading != null)
                {
                    frmLoading.Close();
                    frmLoading = null;
                    GC.Collect();
                }
                if (cHandle != null)
                {
                    cHandle();
                }
            };
            worker.RunWorkerAsync();
            if (loadingImageType != LoadingImageType.None)
            {
                frmLoading = new FrmLoading(loadingImageType);
                if (isModel)
                {
                    frmLoading.ShowDialog();
                }
                else
                {
                    frmLoading.Show();
                }
            }
        }
    }
}

